<template>
    <el-row type="flex" class="row-bg" justify="center">
        <el-col :span="6" style="text-align: center;">
            <H2 style="text-align: center;">欢迎来到VueAdmin</H2>
            <el-image :src="require('@/assets/images/vueAdmin.jpg')"></el-image>
        </el-col>
        <el-col :span="1">
            <el-divider direction="vertical"></el-divider>
        </el-col>
        <el-col :span="6">
            <div class="grid-content bg-purple">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                    class="demo-ruleForm">
                    <el-form-item label="用户名" prop="userName">
                        <el-input v-model="ruleForm.userName" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号" prop="phone">
                        <el-input v-model="ruleForm.phone" placeholder="请输入手机号码" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="验证码" prop="code">
                        <el-input v-model="ruleForm.code" autocomplete="off"></el-input>
                        <el-button type="text" @click="getCode" :disabled="codeDisabled">{{
                            codeMsg
                            }}</el-button>
                    </el-form-item>
                    <el-form-item style="text-align: center;">
                        <el-button type="primary" @click="submitForm('ruleForm')" style="width:100%">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-col>
        <Verify @success="capctchaCheckSuccess" :mode="'pop'" :captchaType="'blockPuzzle'"
            :imgSize="{ width: '330px', height: '155px' }" ref="verify"></Verify>
    </el-row>

</template>
<script>
    import Verify from "@/components/Verifition/Verify";
    import { getSms, login } from "@/api/login";
    export default {
        data() {
            var validatePhone = (rule, value, callback) => {
                if (value == "") {
                    callback(new Error("请输入手机号"));
                } else {
                    if (value !== "") {
                        var reg = /^1[13456789]\d{9}$/;
                        if (!reg.test(value)) {
                            callback(new Error("手机号输入错误"));
                        } else {
                            //手机号码输入正确时才可点击获取验证码按钮
                            this.codeDisabled = false;
                        }
                    }
                    callback();
                }
            };
            return {
                code: "",
                timer: null,
                countdown: 60,
                codeMsg: "获取短信验证码",
                codeDisabled: false,
                ruleForm: {
                    code: "",
                    userName: "",
                    password: "",
                    phone: "",
                    key: "",
                },
                rules: {
                    userName: [
                        { message: "请输入用户名", trigger: "blur" }
                    ],
                    password: [{ message: "请输入密码", trigger: "blur" }],
                    code: [{ message: "请输入验证码", trigger: "blur" }],
                    phone: [{ validator: validatePhone, trigger: "blur" }]
                }
            };
        },
        components: {
            Verify
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        login(this.ruleForm).then(res => {
                            console.log(res);
                            if (res.code == 200) {
                                this.$store.commit("SET_TOKEN", res.msg);
                                this.$router.push('/index');
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: "error"
                                });
                            }
                        })

                    } else {

                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //滑动验证码成功
            capctchaCheckSuccess(data) {
                this.codeDisabled = true;
                getSms(this.ruleForm.phone).then(res => {
                    if (res.start == 200) {
                        this.ruleForm.key = res.key;
                        if (!this.timer) {
                            this.timer = setInterval(() => {
                                let count = this.countdown;
                                if (count > 0 && count <= 60) {
                                    this.countdown = count - 1;
                                    if (this.countdown !== 0) {
                                        this.codeDisabled = true;
                                        this.codeMsg = "重新发送(" + this.countdown + ")";
                                    } else {
                                        clearInterval(this.timer);
                                        this.codeMsg = "获取短信验证码";
                                        this.countdown = 60;
                                        this.timer = null;
                                        this.codeDisabled = false;
                                    }
                                }
                            }, 1000);
                        }
                    } else {
                        this.$message({
                            message: "获取验证码失败，请稍后再试",
                            type: "error"
                        });
                        that.codeDisabled = false;
                    }
                });
            },
            //获取验证码
            getCode() {
                let that = this;
                if (this.ruleForm.phone !== "") {
                    let data = {
                        phoneNum: this.phoneNum
                    };
                    this.$refs.verify.show();
                }else{
                    this.$message({
                            message: "请输入手机号",
                            type: "error"
                    });
                }
            }
        }
    };
</script>
<style>
    .el-divider {
        height: 200px;
    }

    .el-row {
        display: flex;
        height: 100%;
        align-items: center;
    }
</style>